package com.soft.idc.common.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.soft.idc.common.model.IdcMenu;

public class IdcMenuService extends IdcMenu {

	public static List<Record> loadMenu(String userId) {
		String sql = "select m.* from idc_menu m " + "join idc_auth_menu_rel r on r.menu_id = m.menu_id "
				+ "join idc_auth_template a on a.auth_id = r.auth_id " + "join idc_role ro on ro.auth_id = a.auth_id "
				+ "join idc_user u on u.role_id = ro.role_id " + "where m.type=0 and u.user_id = ? "
				+ "order by m.seq asc ";
		List<Record> result = Db.find(sql, userId);

		List<Record> col1 = new ArrayList<Record>();
		Map<Integer, Record> map = new HashMap<Integer, Record>();
		for (int i = 0; i < result.size(); i++) {
			Record vo = result.get(i);
			if (vo.getInt("parent_id") == -1) {// 表示父级
				col1.add(vo);
				map.put(vo.getInt("menu_id"), vo);
			} else {
				Record parentMenu = map.get(vo.getInt("parent_id"));// 从临时map中获取出1级栏目
				List<Record> subMenuList = parentMenu.get("subMenu", null);// 查看父级栏目有没子栏目
				if (subMenuList == null || subMenuList.isEmpty()) {
					subMenuList = new ArrayList<Record>();
					parentMenu.set("subMenu", subMenuList);// 给父级栏目添加子栏目list
				}
				subMenuList.add(vo);// 将当前栏目对象放在子栏目当中
			}
		}
		map.clear();

		return col1;

	}

	public static Map<String,String> queryAuthCode(String userId){
		String sql = "select m.code,m.name from idc_menu m " + "join idc_auth_menu_rel r on r.menu_id = m.menu_id "
				+ "join idc_auth_template a on a.auth_id = r.auth_id " + "join idc_role ro on ro.auth_id = a.auth_id "
				+ "join idc_user u on u.role_id = ro.role_id " + "where m.type=1 and u.user_id = ? " + "order by m.seq asc ";
		List<Record> result = Db.find(sql, userId);
		
		Map<String,String> map = new HashMap<String,String>();
		for(Record vo :result){
			map.put(vo.getStr("code"), vo.getStr("name"));
		}
		return map;
	}

	public static List<Record> queryAllMenu() {
		String sql = "SELECT M.MENU_ID, M.NAME FROM IDC_MENU M";
		List<Record> result = Db.find(sql);
		return result;
	}

	public static List<Record> queryParentMenu() {
		String sql = "SELECT M.MENU_ID, M.NAME FROM IDC_MENU M WHERE M.PARENT_ID=-1 ";
		List<Record> result = Db.find(sql);
		return result;
	}

	public static void main(String[] args) {
		PropKit.use("a_little_config.txt");
		C3p0Plugin dp = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.addMapping("idc_menu", IdcMenu.class);

		// 与web环境唯一的不同是要手动调用一次相关插件的start()方法
		dp.start();
		arp.start();

		// 通过上面简单的几行代码，即可立即开始使用

		String sql = "select menu_id,u.username from idc_auth_menu_rel r,idc_user u,idc_role i where r.auth_id = i.auth_id and u.role_id = i.role_id and u.user_id = ?";

		List<Record> list = IdcMenuService.loadMenu("10000");
		System.out.println(">>> " + JsonKit.toJson(list));

	}
}
